package ms.unicorn.language.languagedef;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;

import ms.unicorn.language.languagedef.impl.LanguagedefFactoryImpl;

/**
 * Replace the default factory of LanguagedefPackageImpl()
 * @author hungnv
 *
 */
public class Factory extends LanguagedefFactoryImpl {

	public static Factory eINSTANCE = new Factory();
	
	private Map<Integer, Class<?>> map = new HashMap<Integer, Class<?>>();
	
	public Factory() {

	}
	
	public void registerSubClass(int classifierID, Class<EObject> clazz) {
		map.put(classifierID, clazz);
	}

	@Override
	public EObject create(EClass eClass) {
		if (map.containsKey(eClass.getClassifierID())) {
			Class<?> cls = map.get(eClass.getClassifierID());
			try {
				return (EObject) cls.newInstance();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return super.create(eClass);
	}
}
